package com.wanda.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanda.model.ContractModel;
import com.wanda.model.JsonGeneralModel;
import com.wanda.model.PayMentModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.ContractService;
import com.wanda.service.PayMentService;


@Controller
@RequestMapping("qt/payment") 
public class PayMentController {
	
	@Resource
	private PayMentService payMentService;
	
	@Resource
	private ContractService contractService;
	
	
  
	//添加客户 
	@RequestMapping(value = "/addPayMent.do")
	public @ResponseBody Map<String, Object> add(String contract_id,String payMent_name, float payMent_money,HttpServletRequest request) {
		PayMentModel pm = new PayMentModel();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		pm.setDelete_state("0");
        pm.setPayment_money(payMent_money);
        pm.setPayment_time(df.format(new Date()));
        Wd_UserModel user = (Wd_UserModel)request.getSession().getAttribute("bean");
        pm.setUser_id(user.getUser_id());
        pm.setContract_id(contract_id);
        pm.setPayment_name(payMent_name);
        String sql = "and wd_payment.contract_id ='"+contract_id+"'";
        List<PayMentModel> lp = payMentService.selectBySql(null, null,sql);
        float yue = 0;
        for(PayMentModel p:lp){
        	yue=+p.getPayment_money();
       
        }
        if(!(lp.isEmpty())){
        	 pm.setSurplus((lp.get(0).getTotal())-yue-payMent_money);
        }else{
         List<ContractModel> model = contractService.selectBySql("and contract_id='"+contract_id+"'");
         pm.setSurplus((model.get(0).getTotal())-payMent_money);
         System.out.println(model.get(0).getTotal());
         System.out.println(payMent_money);
        }
      
        payMentService.addPayMent(pm);
    	Map<String,Object> map = new HashMap<String,Object>();  
    	map.put("msg","成功");
    	return map;
    }
	

		
		//删除客户
		@RequestMapping(value="/deletePayMent.do")
			 public @ResponseBody Map<String,Object> delete(String id){
				      
					PayMentModel model=new PayMentModel();
					model.setId(id);
					payMentService.deletePayMent(model);
			    	Map<String,Object> map = new HashMap<String,Object>();  
			    	System.out.println("删除成功"); 
			    	;
			    	return map;
			    }
				

		//拼接sql查询
				 @RequestMapping(value="/selectPayMent.do")
				    public @ResponseBody JsonGeneralModel selectBySql(int page,int rows,String SearchSql){
					 System.err.println("SearchSql:"+SearchSql);
						JsonGeneralModel<PayMentModel> jsonModel = new JsonGeneralModel();
						List<PayMentModel> customers =null;
						if(page>0){
							page = page-1;
						}
						try {
							customers=payMentService.selectBySql((page)+"",rows+"",SearchSql);
							jsonModel.setRows(customers);
						}catch (Exception e) {
							e.printStackTrace();
						}
						return jsonModel;  
				    }	 
					
}
